package org.jeecg.modules.phone.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.sdk.service.cloudphone20201230.models.RunInstancesResponse;
import com.aliyun.sdk.service.cloudphone20201230.models.RunInstancesResponseBody;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.phone.entity.PhoneOrder;
import org.jeecg.modules.phone.entity.PhoneOrderPamarsVo;
import org.jeecg.modules.phone.service.IPhoneOrderService;
import java.util.Date;
import java.util.concurrent.ExecutionException;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

 /**
 * @Description: 购买手机订单
 * @Author: jeecg-boot
 * @Date:   2022-08-10
 * @Version: V1.0
 */
@Slf4j
@Api(tags="购买手机订单")
@RestController
@RequestMapping("/phone/phoneOrder")
public class PhoneOrderController extends JeecgController<PhoneOrder, IPhoneOrderService> {
	@Autowired
	private IPhoneOrderService phoneOrderService;



	/**
	 * 分页列表查询
	 *
	 * @param phoneOrder
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "购买手机订单-分页列表查询")
	@ApiOperation(value="购买手机订单-分页列表查询", notes="购买手机订单-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(PhoneOrder phoneOrder,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<PhoneOrder> queryWrapper = QueryGenerator.initQueryWrapper(phoneOrder, req.getParameterMap());
		Page<PhoneOrder> page = new Page<PhoneOrder>(pageNo, pageSize);
		IPage<PhoneOrder> pageList = phoneOrderService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 * 添加
	 *
	 * @param phoneOrderPamarsVo
	 * @return
	 */
	@AutoLog(value = "购买手机订单-添加")
	@ApiOperation(value="购买手机订单-添加", notes="购买手机订单-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody PhoneOrderPamarsVo phoneOrderPamarsVo) throws ExecutionException, InterruptedException {
//		记录传参，记录结果。记录实例并查询实例。填满实例
//		{
//			"amount": "1",
//				"chargeType": "PostPaid",
//				"description": "testPho1",
//				"imageId": "android_9_0_0_release_3285106_20220531.raw",
//				"instanceName": "testP",
//				"instanceType": "ecp.ce.large",
//				"keyPairName": "yunphone1",
//				"regionId": "cn-hangzhou",
//				"securityGroupId": "sg-bp1ggdbov2o3nvybkoey",
//				"vswitchId": "vsw-bp1f5diq3vbh9dw4frryq"
//		}
//		PhoneOrder phoneOrder  = new PhoneOrder();
//		RunInstancesResponse res = phoneService.doRunInstances(phoneOrderPamarsVo);
//		phoneOrder.setRequestId(res.getBody().getRequestId());
//		phoneOrder.setInstanceIds(res.getBody().getInstanceIds().getInstanceId().get(0));
//		phoneOrderService.save(phoneOrder);
//		String listInstances = phoneService.doListInstances(phoneOrder.getInstanceIds());
//		phoneService.DeleteInstances(phoneOrder.getInstanceIds());
//		{"headers":{"Access-Control-Allow-Origin":"*","x-acs-request-id":"680C09B0-8668-52EA-AA87-F92CE20DF633","Access-Control-Allow-Methods":"POST, GET, OPTIONS, PUT, DELETE","Connection":"keep-alive","Content-Length":"760","Access-Control-Max-Age":"172800","Date":"Thu, 11 Aug 2022 04:04:08 GMT","Access-Control-Allow-Headers":"X-Requested-With, X-Sequence, _aop_secret, _aop_signature, x-acs-action, x-acs-version, x-acs-date, Content-Type","Content-Type":"application/json;charset\u003dutf-8","x-acs-trace-id":"4ad143d902dc101ad7de7bcb185fd799"},"body":{"instances":{"instance":[{"chargeType":"PostPaid","creationTime":"2022-08-11T04:03Z","description":"testPho1","eipAddress":{"allocationId":"","internetChargeType":"","ipAddress":""},"imageId":"android_9_0_0_release_3285106_20220531.raw","instanceId":"cp-bp11lixgwk59oya1h9y0","instanceName":"testP","instanceType":"ecp.ce.large","keyPairName":"yunphone1","osName":"Android 9.0","osNameEn":"Android 9.0","regionId":"cn-hangzhou","resolution":"536*960","securityGroupId":"sg-bp1ggdbov2o3nvybkoey","status":"Running","vpcAttributes":{"privateIpAddress":"192.168.0.3","vSwitchId":"vsw-bp1f5diq3vbh9dw4frryq"},"zoneId":"cn-hangzhou-i"}]},"maxResults":100,"nextToken":"AAAAAa88Lu+Q2dTGFbnMGD5nD58\u003d","requestId":"680C09B0-8668-52EA-AA87-F92CE20DF633","totalCount":1}}
//		{"instance":[{"chargeType":"PostPaid","creationTime":"2022-08-11T04:03Z","description":"testPho1","eipAddress":{"allocationId":"","internetChargeType":"","ipAddress":""},"imageId":"android_9_0_0_release_3285106_20220531.raw","instanceId":"cp-bp11lixgwk59oya1h9y0","instanceName":"testP","instanceType":"ecp.ce.large","keyPairName":"yunphone1","osName":"Android 9.0","osNameEn":"Android 9.0","regionId":"cn-hangzhou","resolution":"536*960","securityGroupId":"sg-bp1ggdbov2o3nvybkoey","status":"Running","vpcAttributes":{"privateIpAddress":"192.168.0.3","vSwitchId":"vsw-bp1f5diq3vbh9dw4frryq"},"zoneId":"cn-hangzhou-i"}]}
		return Result.OK("添加成功！");
	}
	
	/**
	 * 编辑
	 *
	 * @param phoneOrder
	 * @return
	 */
	@AutoLog(value = "购买手机订单-编辑")
	@ApiOperation(value="购买手机订单-编辑", notes="购买手机订单-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<?> edit(@RequestBody PhoneOrder phoneOrder) {
		phoneOrderService.updateById(phoneOrder);
		return Result.OK("编辑成功!");
	}
	
	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "购买手机订单-通过id删除")
	@ApiOperation(value="购买手机订单-通过id删除", notes="购买手机订单-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		phoneOrderService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "购买手机订单-批量删除")
	@ApiOperation(value="购买手机订单-批量删除", notes="购买手机订单-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.phoneOrderService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功！");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "购买手机订单-通过id查询")
	@ApiOperation(value="购买手机订单-通过id查询", notes="购买手机订单-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		PhoneOrder phoneOrder = phoneOrderService.getById(id);
		return Result.OK(phoneOrder);
	}

  /**
   * 导出excel
   *
   * @param request
   * @param phoneOrder
   */
  @RequestMapping(value = "/exportXls")
  public ModelAndView exportXls(HttpServletRequest request, PhoneOrder phoneOrder) {
      return super.exportXls(request, phoneOrder, PhoneOrder.class, "购买手机订单");
  }

  /**
   * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
  @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
  public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      return super.importExcel(request, response, PhoneOrder.class);
  }

}
